<?php

class Test extends Onecore_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("elem_test_model");
        $this->_moduleName = utf8_encode("Modulo modulos_model");
        $this->_moduleDescription = utf8_encode("Description") . $this->_moduleName;
        $this->_modelInstance = $this->elem_test_model;
    }
    
    public function add() {
        $this->load->model("objects_model");
        $success = false;
        $itemName = $this->_modelInstance->getItemName();
        $mensaje = $itemName . " ingresada en forma exitosa";
        $errorList = array();
        
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $objects_id = $this->input->post('objects_id');
        $type_objects_id = $this->input->post('type_objects_id');
        $_object = null;
//        $userEmail = $this->session->userdata('uid');
        
        $this->form_validation->set_rules($this->_modelInstance->getValidations());
        
        if ($this->form_validation->run() == FALSE) {
            $mensaje = "Campos inválidos";
            $errorList = $this->form_validation->_error_array;
        } else {
            $data = array(
                "id" => "",
                "data" => array(
                    'title' => $title,
                    'description' => $description,
                    "objects_id" => $objects_id,
                    "type_objects_id" => $type_objects_id,
                )
            );

            if ($this->_modelInstance->save($data, false)) {
                $_object = $this->objects_model->getFull($objects_id);
                $success = true;
            } else {
                $mensaje = "La Prueba no pudo ser creada. Verifique los siguientes errores.";
                $errorList = array("Error" => $this->_modelInstance->getError());
            }
        }
        $data = array(
            'success' => $success,
            "msg" => $mensaje,
            "data"=>array(
                "object"=>$_object
            ),
            "errors" => $errorList,
        );
        $this->layout->printLike(json_encode($data), "json");
    }
    
    public function edit() {
        $success = false;
        $itemName = $this->_modelInstance->getItemName();
        $mensaje = $itemName . " editada en forma exitosa";
        $errorList = array();
        
        $id = $this->input->post('id');
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $objects_id = $this->input->post('objects_id');
        $type_objects_id = $this->input->post('type_objects_id');
        $userEmail = $this->session->userdata('uid');
        
        $this->form_validation->set_rules($this->_modelInstance->getValidations());
        
        if ($this->form_validation->run() == FALSE) {
            $mensaje = "Campos inválidos";
            $errorList = $this->form_validation->_error_array;
        } else {
            $data = array(
                "id" => array(
                    'id'=>$id
                ),
                "data" => array(
//                    'umodified'=>$userEmail,
                    'title' => $title,
                    'description' => $description,
                    "objects_id" => $objects_id,
                    "type_objects_id" => $type_objects_id,
                )
            );
            
            if ($this->_modelInstance->save($data, true)) {
                $success = true;
            } else {
                $mensaje = "La Prueba no pudo ser editada. Verifique los siguientes errores.";
                $errorList = array("Error" => $this->_modelInstance->getError());
            }
        }
        $data = array(
            'success' => $success,
            "msg" => $mensaje,
            "errors" => $errorList,
        );
        $this->layout->printLike(json_encode($data), "json");
    }
    //se encarga de evaluar tiempo empleado en ppt y validaciones de historicos..
    /*public function canExecute(){
        $success = false;
        $itemName = $this->_modelInstance->getItemName();
        $mensaje = $itemName . " editada en forma exitosa";
        $errorList = array();
        
        $id = $this->input->post('id');
    }*/
    
}